<?php
namespace app\index\controller;
use think\Controller;
use think\Validate;
use think\Db;
use think\Session;
class Index extends Basic
{  

	
    public function index()
    {


        //预热中
         $date['status']='0';
		
       $project= Db::name('project')->where($date)->order('id desc')->select();;
	   $this->assign('project',$project);
       $nuws=Db::name('newsabount')->order('id desc')->limit('3')->select();; $this->assign('news',$nuws);
      $vehicle_label= Db::name('vehicle_label')->select();
	  foreach($vehicle_label as $va){
	    $date['cheshang_id']=$va['id'];
        $project= Db::name('project')->where($date)->order('xmpx desc')->limit(10)->select();
		$project=sumorder($project);
		$arr[$va['type']]=$project;
		
	  }
	  
        
	  
		
       
		  $configf= Db::name('zcconfig')->find();
              $this->assign('configf',$configf); 
		
	 $this->assign('project',$arr);

	 

	 
     return $this->fetch();
    }
  public function zxkf(){
  $config= Db::name('config')->find();
		
		 $this->assign("config",$config);
   return $this->fetch();
  }
	public function login()
    {//登录 
		 
		  $post=input("post.");//获取post数据'

		  if($post){
					 
	         if($post['name']==''){ exit(json_encode(array('status'=>'0','msg'=>'用户名不能为空')));}
			  if($post['pwd']==''){exit(json_encode(array('status'=>'0','msg'=>'密码不能为空')));}
			 // if(!captcha_check($post['vertify'])){exit(json_encode(array('status'=>'0','msg'=>'验证码错误')));};

			$user= Db::name('member')->where('name',$post['name'])->find();
			if(!$user){exit(json_encode(array('status'=>'0','msg'=>'用户名错误')));}
			if($user['state']!='1'){exit(json_encode(array('status'=>'0','msg'=>'该会员已锁定，请联系管理员')));}
    
			if(md5($post['pwd'])!=$user['pwd']){exit(json_encode(array('status'=>'0','msg'=>'密码错误')));}
			Session::set('userid',$user['id']);
			$data['logintime']=date("Y-m-d H:i:s");
			$log['userid']=$user['id'];
			$log['ip']=httpip();
			$log['addtime']=$data['logintime'];
				$insert=Db::name('memberlog')->insert($log);
            Db::name('member')->where('name',$post['name'])->update($data);
			exit(json_encode(array('status'=>'1','url'=>'index')));


		 }else{
		  
          return $this->fetch();
		  }
    }
	public function regvertifyf(){//注册验证码
	      $post=input("post.");//获取post数据'
	 
		  if($post){
		  
		  if(!captcha_check($post['ver'],'',['reset'=>false])){exit(json_encode(array('status'=>'0','msg'=>'验证码错误')));};
		  exit(json_encode(array('status'=>'1','msg'=>'')));
		  }
	
	
	}
	public function reg()
    {//注册
		 
		  $post=input("post.");//获取post数据'
	 
		  if($post){
			 // return json(Session::get('yzm'));
			   //exit(json_encode(array('status'=>'0','msg'=>$post['mobile'])));
	          if($post['name']==''){ exit(json_encode(array('status'=>'0','msg'=>'用户名不能为空')));}
			  if( $post['pwd']==''){return json(array('status'=>'0','msg'=>'密码不能为空'));}
			  if($post['pwd']==''){exit(json_encode(array('status'=>'0','msg'=>'密码不能为空')));}
			  $post['pwd']=MD5($post['pwd']);
			  if($post['vertify']==''){exit(json_encode(array('status'=>'0','msg'=>'验证码不能为空')));}
              if($post['vertify']!=Session::get('yzm')){exit(json_encode(array('status'=>'0','msg'=>'验证码错误')));}

			  if($post['inviter']!=''){//验证推荐人
			  $inviter= Db::name('member')->where('id',$post['inviter'])->find();
               if(!$inviter  or $inviter['name']==$post['name']){
			   exit(json_encode(array('status'=>'0','msg'=>'推荐人错误')));
			   }
                $post['inviter']=$inviter['id'];
				$post['inviterarr']=$inviter['id'];
				if(!empty($inviter['inviterarr'])){
				$post['inviterarr']=$post['inviterarr'].",".$inviter['inviterarr'];
				
				}

			  }
			  unset($post['vertify']);
			  if($post['mobile']==''){exit(json_encode(array('status'=>'0','msg'=>'手机不能为空')));}
			  if(!preg_match("/^1[345678]{1}\d{9}$/",$post['mobile'])){exit(json_encode(array('status'=>'0','msg'=>'手机号格式错误')));}
			 if(!preg_match('/^[0-9a-zA-Z]+$/', $post['name'])){ exit(json_encode(array('status'=>'0','msg'=>'用户名为数字和字母')));}
			$user= Db::name('member')->where('name',$post['name'])->find();
			if($user){exit(json_encode(array('status'=>'0','msg'=>'用户名已存在')));}
			$user= Db::name('member')->where('mobile',$post['mobile'])->find();
			if($user){exit(json_encode(array('status'=>'0','msg'=>'手机号已存在')));}
			$post['paypwd']=$post['pwd'];
			$post['regtime']=date("Y-m-d H:i:s");
			$date=array();
			$date['pwd']=$post['pwd'];
			$date['paypwd']=$post['paypwd'];
			$date['regtime']=$post['regtime'];
			

			$date['inviter']=$post['inviter'];
			if($date['inviter']!=''){$date['inviterarr']=$post['inviterarr'];}
			$date['name']=$post['name'];
			$date['mobile']=$post['mobile'];
			$date['qq']=$post['qq'];
			
			$insert=Db::name('member')->insert($date);
            $userId = Db::name('member')->getLastInsID();
			 $config= Db::name('config')->find();
			 if($config['xslb']>0){
				 $date['amount']=$config['xslb'];
                  handlelog($post['inviter'],$config['xslb'],'+',8,'推荐会员-'.$date['name']);
				  $con="获得推荐礼包".$config['xslb']."元";
				  //addnews('恭喜你获得推荐奖',$con,$post['inviter']);

				   $ab="尊敬的".$date['name']."会员您好！！欢迎您注册使用本网站";
				  $con="您好，恭喜您注册成为".$config['title']."会员，".$config['title']."将为您提供优质服务。";
				  addnews($ab,$con,$userId);
				 }
			if($insert)exit(json_encode(array('status'=>'1','url'=>'/index/index/login')));
			else exit(json_encode(array('status'=>'0','msg'=>'未知错误')));


		 }else{
		      $get=input("param.");
			  $id='';
			  if(!empty($get['id'])){
			  $id=$get['id'];
			  }else{
			    $id='';exit();
			  }$this->assign('id',$id);
			  return $this->fetch();
		  }
    }

	

	public function logout(){
	Session::set('userid','');
	header('Location: index');die;
	}

	public function zhpwd(){//找回密码


	  $post=input("post.");//获取post数据'
	 
		  if($post){
			   //exit(json_encode(array('status'=>'0','msg'=>$post['mobile'])));
	          if($post['name']==''){ exit(json_encode(array('status'=>'0','msg'=>'用户名不能为空')));}
			 
			  if($post['vertify']==''){exit(json_encode(array('status'=>'0','msg'=>'验证码不能为空')));}
			 
              if(!captcha_check($post['vertify'])){exit(json_encode(array('status'=>'0','msg'=>'验证码错误')));}

			
			  unset($post['vertify']);
			  if($post['mobile']==''){exit(json_encode(array('status'=>'0','msg'=>'手机不能为空')));}
			  if(!preg_match("/^1[345678]{1}\d{9}$/",$post['mobile'])){exit(json_encode(array('status'=>'0','msg'=>'手机号格式错误')));}
			 if(!preg_match('/^[0-9a-zA-Z]+$/', $post['name'])){ exit(json_encode(array('status'=>'0','msg'=>'用户名为数字和字母')));}
			$user= Db::name('member')->where('name',$post['name'])->find();
		     if(!$user){ exit(json_encode(array('status'=>'0','msg'=>'用户名错误')));}
			
			if($user['mobile']!=$post['mobile']){ exit(json_encode(array('status'=>'0','msg'=>'用户手机错误')));}
              $post['pwd']=sendsmsfs($user['mobile']);
              if(!$post['pwd']){exit(json_encode(array('status'=>'0','msg'=>'短信发送失败！')));}
			 $post['pwd']=MD5($post['pwd']);
			 $data['pwd']=$post['pwd'];
			 $data['paypwd']=$post['pwd'];
			$insert= Db::name('member')->where('id',$user['id'])->update($data);
			if($insert)exit(json_encode(array('status'=>'1','url'=>'updxcg')));
			else exit(json_encode(array('status'=>'0','msg'=>'未知错误')));
		 }else{
		  
          return $this->fetch();
		  }
	}
   public function updxcg(){
   
    return $this->fetch();
   }

   public function zcxy(){
   
    return $this->fetch();
   }
	public function gl(){
	  $type=input('param.type');
	  $arr=web($type);
	  
		$this->assign('arr',$arr); 
		
     return $this->fetch();
	  
	}

	public function fh(){
        amount_to_money();

    }

}
